// enterpriseMine/post/addPost/addPost.js
import {
    ajaxPost,
    showToast
} from "../../../utils/util";
Page({
    /**
     * 页面的初始数据
     */
    data: {
        type: "",
        region: ["全部", "全部", "全部"], // 工作地点
        customItem: '全部', // 省市区 第一个选项

        workTypeArr: [], // 全部展示
        newWorkTypeArrIndex: [0, 0],
        newWorkTypeArr: [], //展示内容
        workVal: [], //展示内容

        postArr: [], // 全部展示
        newPostArrIndex: [0, 0],
        newPostArr: [], //期望行业
        postVal: [], //期望行业展示内容

        moneyIndex: [0, 0], //薪资范围 key
        moneyArr: [], //薪资范围
        moneyVal: [], //薪资范围展示

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.type == "add") {
            wx.setNavigationBarTitle({
                title: "发布意向"
            });
        } else {
            wx.setNavigationBarTitle({
                title: "编辑意向"
            });
            let data = {
                token: wx.getStorageSync("token"),
                intention_id: options.Id,
            };
            ajaxPost("/authuser/jobintentioninfo", data, res => {
                if (res.code == 0) {
                    let addArr = []
                    addArr.push(res.data.province);
                    addArr.push(res.data.city);
                    addArr.push(res.data.district);
                    this.setData({
                        id: options.Id,
                        workVal: res.data.position,
                        postVal: res.data.industry,
                        moneyVal: res.data.salary,
                        region: addArr,
                    })
                }
            });
        }
        let data = {
            token: wx.getStorageSync("token")
        };
        ajaxPost("/company/positionConfig", data, res => {
            let {
                company_industry,
                position_salary,
                position_nature,
                position_type,
                position_major,
                position_salary_top
            } = res.data.config_list;
            if (res.code == 0) {
                /**
                 * 期望行业
                 */
                let newWorkOneArr = [];
                for (let i in position_type) {
                    newWorkOneArr.push(position_type[i].name);
                }
                let newWorkTwoArr = [];
                let workTwoArr = position_type[0].data;
                for (let i in workTwoArr) {
                    newWorkTwoArr.push(workTwoArr[i].name);
                }
                let newWorkTypeArr = [newWorkOneArr, newWorkTwoArr];
                /**
                 * 期望职位
                 */
                let newPostOneArr = [];
                for (let i in company_industry) {
                    newPostOneArr.push(company_industry[i].name);
                }
                let newPostTwoArr = [];
                let postTwoArr = company_industry[0].data;
                for (let i in postTwoArr) {
                    newPostTwoArr.push(postTwoArr[i].name);
                }
                let newPostArr = [newPostOneArr, newPostTwoArr];
                /**
                 * 薪酬范围
                 */
                let newMoneyArr = [position_salary, position_salary_top];
                this.setData({
                    workTypeArr: position_type,
                    newWorkTypeArr: newWorkTypeArr,

                    postArr: company_industry,
                    newPostArr: newPostArr,

                    moneyArr: newMoneyArr, // 薪资范围
                    majorArrAll: position_major, // 专业需求总列表
                    type: options.type
                });
            }
        });
    },
    /**
     *  职位类型二级分类选择监听事件
     * @param {*} 穿参
     */
    workListChange(e) {
        if (e.detail.column == 0) {
            let workTypeArr = this.data.workTypeArr;
            let newTwoArr = [];
            let arrTwo = workTypeArr[e.detail.value].data;
            for (let i in arrTwo) {
                newTwoArr.push(arrTwo[i].name);
            }
            let Arr = [this.data.newWorkTypeArr[0], newTwoArr];
            let newWorkTypeArrIndex = [e.detail.value, 0];
            this.setData({
                newWorkTypeArr: Arr,
                newWorkTypeArrIndex: newWorkTypeArrIndex
            });
        }
    },

    /**
     * 期望职位确认
     */
    changeWork(e) {
        let index = e.detail.value;
        let newWorkArr = [];
        newWorkArr.push(this.data.workTypeArr[index[0]].name);
        newWorkArr.push(this.data.workTypeArr[index[0]].data[index[1]].name);
        this.setData({
            workVal: newWorkArr
        });
    },
    /**
     *  期望行业二级分类选择监听事件
     * @param {*} 穿参
     */
    postListChange(e) {
        if (e.detail.column == 0) {
            let postArr = this.data.postArr;
            let newTwoArr = [];
            let arrTwo = postArr[e.detail.value].data;
            for (let i in arrTwo) {
                newTwoArr.push(arrTwo[i].name);
            }
            let Arr = [this.data.newPostArr[0], newTwoArr];
            let newPostArrIndex = [e.detail.value, 0];
            this.setData({
                newPostArr: Arr,
                newPostArrIndex: newPostArrIndex
            });
        }
    },
    /**
     * 期望行业确认
     */
    changePost(e) {
        let index = e.detail.value;
        let newPostArr = [];
        newPostArr.push(this.data.postArr[index[0]].name);
        newPostArr.push(this.data.postArr[index[0]].data[index[1]].name);
        this.setData({
            postVal: newPostArr
        });
    },
    /**
     *  薪资范围二级分类选择监听事件
     * @param {*} 穿参
     */
    moneyPickerChange(e) {
        let moneyIndex = this.data.moneyIndex;
        if (e.detail.column == 0) {
            let newMoneyIndex = [e.detail.value, moneyIndex[1]];
            this.setData({
                moneyIndex: newMoneyIndex
            });
        }
        if (e.detail.column == 1) {
            let newMoneyIndex = [moneyIndex[0], e.detail.value];
            this.setData({
                moneyIndex: newMoneyIndex
            });
        }
    },
    /**
     * 薪资范围确认
     */
    changeMoney(e) {
        let index = e.detail.value;
        let newWorkArr = [];
        let moneyArr = this.data.moneyArr;
        newWorkArr.push(moneyArr[0][index[0]]);
        newWorkArr.push(moneyArr[1][index[1]]);
        this.setData({
            moneyVal: newWorkArr
        });
    },

    /**
     * 工作地点选择
     * @param {*} e
     */
    bindRegionChange(e) {
        this.setData({
            region: e.detail.value
        });
    },


    /**
     * 发布岗位
     */
    addData() {
        let {
            workVal, //职位类型
            postVal, //期望行业
            region, //工作地点
            moneyVal, //薪资范围
        } = this.data;
        if (workVal.length == 0) {
            showToast("text", "请选择期望职位");
            return;
        }
        if (moneyVal.length == 0) {
            showToast("text", "请选择期望行业");
            return;
        }
        if (postVal.length == 0) {
            showToast("text", "请选择薪资范围");
            return;
        }
        let data = {
            token: wx.getStorageSync("token"),
            position: workVal,
            industry: postVal,
            salary: moneyVal,
            province: region[0],
            city: region[1],
            district: region[2],
        };
        ajaxPost("/authuser/addJobIntention", data, res => {
            if (res.code == 0) {
                showToast("text", "添加成功");
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1
                    });
                }, 1500);
            }
        });
    },
    /**
     * 删除
     */
    delData() {
        let that = this
        wx.showModal({
            title: '提示',
            content: '确认删除本条求职意向么？',
            success(res) {
                if (res.confirm) {
                    let data = {
                        token: wx.getStorageSync("token"),
                        id: that.data.id,
                    };
                    ajaxPost("/authuser/deleteJobIntention", data, res => {
                        if (res.code == 0) {
                            showToast("text", "删除成功");
                            setTimeout(() => {
                                wx.navigateBack({
                                    delta: 1
                                });
                            }, 1500);
                        }
                    });
                }
            }
        })
    },
    /**
     * 编辑
     */
    upData() {
        let {
            workVal, //职位类型
            postVal, //期望行业
            region, //工作地点
            moneyVal, //薪资范围
        } = this.data;
        if (workVal.length == 0) {
            showToast("text", "请选择期望职位");
            return;
        }
        if (moneyVal.length == 0) {
            showToast("text", "请选择期望行业");
            return;
        }
        if (postVal.length == 0) {
            showToast("text", "请选择薪资范围");
            return;
        }
        let data = {
            token: wx.getStorageSync("token"),
            id: this.data.id,
            position: workVal,
            industry: postVal,
            salary: moneyVal,
            province: region[0],
            city: region[1],
            district: region[2],
        };
        ajaxPost("/authuser/updateJobIntention", data, res => {
            if (res.code == 0) {
                showToast("text", "保存成功");
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1
                    });
                }, 1500);
            }
        });
    },
});